﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SimpleSocket
{
    internal static class Helpers
    {
        /// <summary>
        /// Combines 2 byte arrays into 1
        /// Special Thanks to Jon Skeet for the function.
        /// </summary>
        /// <param name="first"></param>
        /// <param name="second"></param>
        /// <returns></returns>
        public static byte[] Combine(byte[] first, byte[] second)
        {
            byte[] ret = new byte[first.Length + second.Length];
            Buffer.BlockCopy(first, 0, ret, 0, first.Length);
            Buffer.BlockCopy(second, 0, ret, first.Length, second.Length);
            return ret;
        }

        /// <summary>
        /// Combines 3 byte arrays into 1.
        /// Special Thanks to Jon Skeet for the function.
        /// </summary>
        /// <param name="first"></param>
        /// <param name="second"></param>
        /// <param name="third"></param>
        /// <returns></returns>
        public static byte[] Combine(byte[] first, byte[] second, byte[] third)
        {
            byte[] ret = new byte[first.Length + second.Length + third.Length];
            Buffer.BlockCopy(first, 0, ret, 0, first.Length);
            Buffer.BlockCopy(second, 0, ret, first.Length, second.Length);
            Buffer.BlockCopy(third, 0, ret, first.Length + second.Length,
                             third.Length);
            return ret;
        }

        /// <summary>
        /// Combines many byte arrays into 1
        /// Special Thanks to Jon Skeet for the function.
        /// </summary>
        /// <param name="arrays"></param>
        /// <returns></returns>
        public static byte[] Combine(params byte[][] arrays)
        {
            byte[] ret = new byte[arrays.Sum(x => x.Length)];
            int offset = 0;
            foreach (byte[] data in arrays)
            {
                Buffer.BlockCopy(data, 0, ret, offset, data.Length);
                offset += data.Length;
            }
            return ret;
        }

        /// <summary>
        /// Checks to see values contains a bit flag.
        /// </summary>
        /// <param name="value"></param>
        /// <param name="contains"></param>
        /// <returns></returns>
        public static bool HasFlag(MessageFlags value, MessageFlags contains)
        {
            return (value & contains) == contains;
        }

        /// <summary>
        /// Checks to see values contains a bit flag.
        /// </summary>
        /// <param name="value"></param>
        /// <param name="contains"></param>
        /// <returns></returns>
        public static bool HasFlag(MessageFlags value, params MessageFlags[] contains)
        {

            foreach (MessageFlags flag in contains)
            {
                bool result = (value & flag) == flag;
                if (result)
                    return true;
            }

            return false;
        }


    }
}
